package algorithm.leetcode.I601to800;

public class Q724 {

    public int pivotIndex(int[] nums) {
        int leftSum = 0, rightSum = 0;

        for (int i = 1; i < nums.length; i++) {
            rightSum += nums[i];
        }

        for (int i = 0; i < nums.length; i++) {
            if (rightSum == leftSum) return i;
            else {
                leftSum += nums[i];
                rightSum -= (i+1 == nums.length ? 0 : nums[i+1]);
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        Q724 q724 = new Q724();
        System.out.println(q724.pivotIndex(new int[]{2,1,-1}));
    }

}
